package com.sias.mianshiya.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sias.mianshiya.common.ErrorCode;
import com.sias.mianshiya.constant.CommonConstant;
import com.sias.mianshiya.exception.ThrowUtils;
import com.sias.mianshiya.mapper.QuestionMapper;
import com.sias.mianshiya.model.dto.question.QuestionQueryRequest;
import com.sias.mianshiya.model.entity.Question;
import com.sias.mianshiya.model.entity.QuestionBankQuestion;
import com.sias.mianshiya.model.entity.User;
import com.sias.mianshiya.model.vo.QuestionVO;
import com.sias.mianshiya.model.vo.UserVO;
import com.sias.mianshiya.service.QuestionBankQuestionService;
import com.sias.mianshiya.service.QuestionService;
import com.sias.mianshiya.service.UserService;
import com.sias.mianshiya.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 题目服务实现
 *
 * @author <a href="https://github.com/lisias">程序员鱼皮</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

  @Resource
  private UserService userService;

  @Resource
  private QuestionBankQuestionService questionBankQuestionService;

  /**
   * 校验数据
   *
   * @param question
   * @param add      对创建的数据进行校验
   */
  @Override
  public void validQuestion(Question question, boolean add) {
    ThrowUtils.throwIf(question == null, ErrorCode.PARAMS_ERROR);
    // todo 从对象中取值
    String title = question.getTitle();
    String content = question.getContent();

    // 创建数据时，参数不能为空
    if (add) {
      // todo 补充校验规则
      ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
    }
    // 修改数据时，有参数则校验
    // todo 补充校验规则
    if (StringUtils.isNotBlank(title)) {
      ThrowUtils.throwIf(title.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
    }
    if (StringUtils.isNotBlank(content)) {
      ThrowUtils.throwIf(title.length() > 10240, ErrorCode.PARAMS_ERROR, "内容过长");
    }
  }

  /**
   * 获取查询条件
   *
   * @param questionQueryRequest
   * @return
   */
  @Override
  public QueryWrapper<Question> getQueryWrapper(QuestionQueryRequest questionQueryRequest) {
    QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
    if (questionQueryRequest == null) {
      return queryWrapper;
    }
    // todo 从对象中取值
    Long id = questionQueryRequest.getId();
    Long notId = questionQueryRequest.getNotId();
    String searchText = questionQueryRequest.getSearchText();
    String title = questionQueryRequest.getTitle();
    String content = questionQueryRequest.getContent();
    String answer = questionQueryRequest.getAnswer();
    Long userId = questionQueryRequest.getUserId();
    String sortField = questionQueryRequest.getSortField();
    String sortOrder = questionQueryRequest.getSortOrder();
    List<String> tagList = questionQueryRequest.getTags();
    // todo 补充需要的查询条件
    // 从多字段中搜索
    if (StringUtils.isNotBlank(searchText)) {
      // 需要拼接查询条件
      for (String search : searchText.split("\\s+")) {
        // queryWrapper.and(qw -> qw.like("title", search).or().like("content", search).or().like("tags", search));
        queryWrapper
                .or().like("title", search)
                .or().like("content", search)
                .or().like("answer", search)
                .or().like("tags", search)
                .or().like("id", id);
      }
    }
    // 模糊查询
    queryWrapper.or().like(StringUtils.isNotBlank(title), "title", title);
    queryWrapper.or().like(StringUtils.isNotBlank(content), "content", content);
    queryWrapper.or().like(StringUtils.isNotBlank(answer), "answer", answer);
    // JSON 数组查询
    if (CollUtil.isNotEmpty(tagList)) {
      for (String tag : tagList) {
        queryWrapper.or().like("tags", "\"" + tag + "\"");
      }
    }
    // 精确查询
    queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
    queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
    queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
    // 排序规则
    queryWrapper.orderBy(SqlUtils.validSortField(sortField),
            sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
            sortField);
    return queryWrapper;
  }

  /**
   * 获取题目封装
   *
   * @param question
   * @param request
   * @return
   */
  @Override
  public QuestionVO getQuestionVO(Question question, HttpServletRequest request) {
    // 对象转封装类
    QuestionVO questionVO = QuestionVO.objToVo(question);

    // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
    // region 可选
    // 1. 关联查询用户信息
    Long userId = question.getUserId();
    User user = null;
    if (userId != null && userId > 0) {
      user = userService.getById(userId);
    }
    UserVO userVO = userService.getUserVO(user);
    questionVO.setUser(userVO);
    // 2. 已登录，获取用户点赞、收藏状态
    // long questionId = question.getId();
    // User loginUser = userService.getLoginUserPermitNull(request);
    // if (loginUser != null) {
    //     // 获取点赞
    //     QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
    //     questionThumbQueryWrapper.in("questionId", questionId);
    //     questionThumbQueryWrapper.eq("userId", loginUser.getId());
    //     QuestionThumb questionThumb = questionThumbMapper.selectOne(questionThumbQueryWrapper);
    //     questionVO.setHasThumb(questionThumb != null);
    //     // 获取收藏
    //     QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
    //     questionFavourQueryWrapper.in("questionId", questionId);
    //     questionFavourQueryWrapper.eq("userId", loginUser.getId());
    //     QuestionFavour questionFavour = questionFavourMapper.selectOne(questionFavourQueryWrapper);
    //     questionVO.setHasFavour(questionFavour != null);
    // }
    // endregion

    return questionVO;
  }

  /**
   * 分页获取题目封装
   *
   * @param questionPage
   * @param request
   * @return
   */
  @Override
  public Page<QuestionVO> getQuestionVOPage(Page<Question> questionPage, HttpServletRequest request) {
    List<Question> questionList = questionPage.getRecords();
    Page<QuestionVO> questionVOPage = new Page<>(questionPage.getCurrent(), questionPage.getSize(), questionPage.getTotal());
    if (CollUtil.isEmpty(questionList)) {
      return questionVOPage;
    }
    // 对象列表 => 封装对象列表
    List<QuestionVO> questionVOList = questionList.stream().map(QuestionVO::objToVo).collect(Collectors.toList());

    // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
    // region 可选
    // 1. 关联查询用户信息
    Set<Long> userIdSet = questionList.stream().map(Question::getUserId).collect(Collectors.toSet());
    Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
            .collect(Collectors.groupingBy(User::getId));
    // 2. 已登录，获取用户点赞、收藏状态
    // Map<Long, Boolean> questionIdHasThumbMap = new HashMap<>();
    // Map<Long, Boolean> questionIdHasFavourMap = new HashMap<>();
    // User loginUser = userService.getLoginUserPermitNull(request);
    // if (loginUser != null) {
    //     Set<Long> questionIdSet = questionList.stream().map(Question::getId).collect(Collectors.toSet());
    //     loginUser = userService.getLoginUser(request);
    //     // 获取点赞
    //     QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
    //     questionThumbQueryWrapper.in("questionId", questionIdSet);
    //     questionThumbQueryWrapper.eq("userId", loginUser.getId());
    //     List<QuestionThumb> questionQuestionThumbList = questionThumbMapper.selectList(questionThumbQueryWrapper);
    //     questionQuestionThumbList.forEach(questionQuestionThumb -> questionIdHasThumbMap.put(questionQuestionThumb.getQuestionId(), true));
    //     // 获取收藏
    //     QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
    //     questionFavourQueryWrapper.in("questionId", questionIdSet);
    //     questionFavourQueryWrapper.eq("userId", loginUser.getId());
    //     List<QuestionFavour> questionFavourList = questionFavourMapper.selectList(questionFavourQueryWrapper);
    //     questionFavourList.forEach(questionFavour -> questionIdHasFavourMap.put(questionFavour.getQuestionId(), true));
    // }
    // 填充信息
    questionVOList.forEach(questionVO -> {
      Long userId = questionVO.getUserId();
      User user = null;
      if (userIdUserListMap.containsKey(userId)) {
        user = userIdUserListMap.get(userId).get(0);
      }
      questionVO.setUser(userService.getUserVO(user));
      // questionVO.setHasThumb(questionIdHasThumbMap.getOrDefault(questionVO.getId(), false));
      // questionVO.setHasFavour(questionIdHasFavourMap.getOrDefault(questionVO.getId(), false));
    });
    // endregion

    questionVOPage.setRecords(questionVOList);
    return questionVOPage;
  }

  /**
   * 分页获取题目列表（仅管理员可用）
   *
   * @param questionQueryRequest
   * @return
   */
  @Override
  public Page<Question> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest) {
    long current = questionQueryRequest.getCurrent();
    long size = questionQueryRequest.getPageSize();

    // 题目查询
    QueryWrapper<Question> queryWrapper = getQueryWrapper(questionQueryRequest);
    // 根据题库ID查询
    Long questionBankId = questionQueryRequest.getQuestionBankId();
    if (questionBankId != null) {
      LambdaQueryWrapper<QuestionBankQuestion> lambdaQueryWrapper = Wrappers.lambdaQuery(QuestionBankQuestion.class)
              .select(QuestionBankQuestion::getQuestionId)
              .eq(QuestionBankQuestion::getQuestionBankId, questionBankId);
      List<QuestionBankQuestion> questionBankQuestionList = questionBankQuestionService.list(lambdaQueryWrapper);
      if (CollUtil.isNotEmpty(questionBankQuestionList)) {
        // 取出题目 id 集合
        List<Long> questionsId = questionBankQuestionList.stream()
                .map(QuestionBankQuestion::getQuestionId)
                .collect(Collectors.toList());
        // 复用原有题目表的查询条件
        queryWrapper.in("id", questionsId);
      } else {
        return new Page<>(current, size, 0);
      }
    }
    // 查询数据库
    return page(new Page<>(current, size), queryWrapper);
  }

}
